package com.trigsoft.applist;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckedTextView;
import android.widget.CursorAdapter;
import android.widget.ImageView;

public class ExceptionCursorAdapter extends CursorAdapter
{
	static class Wrapper
	{
		ImageView icon;
		CheckedTextView text;
		boolean checked = false;
		String name;
		String pkg;
	}

	private View view;

	public ExceptionCursorAdapter(Context context, Cursor c)
	{
		super(context, c);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor)
	{
		// TODO Auto-generated method stub
		String name = cursor.getString(cursor.getColumnIndex(DBHelper.KEY_LABEL));

		Wrapper wrapper = (Wrapper) view.getTag();
		if (IconCache.icons.containsKey(name))
		{
			wrapper.icon.setImageBitmap(IconCache.icons.get(name));
		} else
		{
			byte[] b = cursor.getBlob(cursor.getColumnIndex(DBHelper.KEY_ICON));
			Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
			IconCache.icons.put(name, bitmap);
			wrapper.icon.setImageBitmap(bitmap);
		}

		wrapper.text.setText(name);

	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent)
	{
		// TODO Auto-generated method stub

		LayoutInflater inflater = ((Activity) context).getLayoutInflater();

		view = inflater.inflate(R.layout.exception_item, null);

		Wrapper wrapper = new Wrapper();
		wrapper.icon = (ImageView) view.findViewById(R.id.icon);
		wrapper.text = (CheckedTextView) view.findViewById(R.id.name);
		wrapper.pkg = cursor.getString(cursor.getColumnIndex(DBHelper.KEY_PKG));
		wrapper.name = cursor.getString(cursor.getColumnIndex(DBHelper.KEY_NAME));
		view.setTag(wrapper);

		return view;
	}
}
